﻿namespace com.stifel.BranchMgmt.WorkflowPromoteInfo
{
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Workflow.Activities;
    using System.Workflow.Runtime;
    using System.Workflow.Runtime.Hosting;

    using com.stifel.BranchMgmtDao;
    using com.stifel.BranchPromotion;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Workflow;

    public sealed partial class WorkflowPromoteInfo : SequentialWorkflowActivity
    {
        #region Fields

        public Guid workflowId = default( System.Guid );
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties( );

        StifelBranchConfiguration _Configuration;
        BranchPromotion _Promotion;

        #endregion Fields

        #region Constructors

        public WorkflowPromoteInfo( )
        {
            InitializeComponent( );
        }

        #endregion Constructors

        #region Properties

        public Guid CurrentRequest
        {
            get;
            set;
        }

        public SPUserInfoExpanded CurrentUser
        {
            get;
            set;
        }

        public SPWeb CurrentWeb
        {
            get;
            set;
        }

        StifelBranchConfiguration Configuration
        {
            get
            {
                if ( _Configuration == null )
                {
                    StifelBranchConfigurationDao dao = new StifelBranchConfigurationDao( CurrentWeb.Url );
                    _Configuration = dao.Get( );
                }
                return _Configuration;
            }
        }

        BranchPromotion Promotion
        {
            get
            {
                if ( _Promotion == null )
                {
                    _Promotion = new BranchPromotion( CurrentRequest, CurrentUser, CurrentWeb, Configuration.ProductionSiteURLForTheLiveBranchesUrl );
                }
                return _Promotion;
            }
        }

        #endregion Properties

        #region Methods

        protected class Parms
        {
            public Guid Request
            {
                get;
                set;
            }
            public SPWeb Web
            {
                get;
                set;
            }
            public SPUserInfoExpanded User
            {
                get;
                set;
            }
        }

        public static void Start( Guid uniqueId, SPWeb spWeb, SPUserInfoExpanded user )
        {
            ThreadPool.QueueUserWorkItem( start, new Parms
            {
                Request = uniqueId,
                User = user,
                Web = spWeb
            } );

            //Dictionary<string, object> parameters = new Dictionary<string, object>( );
            //parameters.Add( "CurrentRequest", uniqueId );
            //parameters.Add( "CurrentUser", user );
            //parameters.Add( "CurrentWeb", spWeb );

            //ManualWorkflowSchedulerService service = new ManualWorkflowSchedulerService( );
            //WorkflowRuntime runtime = new WorkflowRuntime( );
            //runtime.AddService( service );
            //WorkflowInstance instance = runtime.CreateWorkflow( typeof( WorkflowPromoteInfo ), parameters );
            //instance.Start( );
            //var scheduler = runtime.GetService<ManualWorkflowSchedulerService>( );
            //scheduler.RunWorkflow( instance.InstanceId );
        }

        static void start( object o )
        {
            Parms parms = o as Parms;
            Dictionary<string, object> parameters = new Dictionary<string, object>( );
            parameters.Add( "CurrentRequest", parms.Request );
            parameters.Add( "CurrentUser", parms.User );
            parameters.Add( "CurrentWeb", parms.Web );

            ManualWorkflowSchedulerService service = new ManualWorkflowSchedulerService( );
            WorkflowRuntime runtime = new WorkflowRuntime( );
            runtime.AddService( service );
            WorkflowInstance instance = runtime.CreateWorkflow( typeof( WorkflowPromoteInfo ), parameters );
            instance.Start( );
            var scheduler = runtime.GetService<ManualWorkflowSchedulerService>( );
            scheduler.RunWorkflow( instance.InstanceId );
        }

        private void branchInfo_copy( object sender, EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.Info );
        }

        private void branchInfo_delete( object sender, EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.Info );
        }

        private void branchLease_copy( object sender, System.EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.Lease );
        }

        private void branchLease_delete( object sender, System.EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.Lease );
        }

        private void branchPersonnel_copy( object sender, EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.Personnel );
        }

        private void branchPersonnel_delete( object sender, System.EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.Personnel );
        }

        private void branchUnit_copy( object sender, EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.Unit );
        }

        private void branchUnit_delete( object sender, EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.Unit );
        }

        private void branchUpdate_copy( object sender, System.EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.Update );
        }

        private void branchUpdate_delete( object sender, System.EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.Update );
        }

        private void branchVendor_copy( object sender, System.EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.Vendor );
        }

        private void branchVendor_delete( object sender, System.EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.Vendor );
        }

        private void hardware_copy( object sender, System.EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.Hardware );
        }

        private void hardware_delete( object sender, System.EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.Hardware );
        }

        private void networkEquipment_copy( object sender, System.EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.NetworkEquipment );
        }

        private void networkEquipment_delete( object sender, System.EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.NetworkEquipment );
        }

        private void photo_copy( object sender, EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.Photo );
        }

        private void photo_delete( object sender, EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.Photo );
        }

        private void telephoneSystem_copy( object sender, EventArgs e )
        {
            Promotion.Copy( BranchPromotionArea.TelephoneSystem );
        }

        private void telephoneSystem_delete( object sender, EventArgs e )
        {
            Promotion.Delete( BranchPromotionArea.TelephoneSystem );
        }

        #endregion Methods

        private void WorkflowPromoteInfo_Initialized( object sender, EventArgs e )
        {
            CustomLoggingService.LogMessage( CustomLoggingService.Area.WorkflowInfrastructure, "Branch promotion workflow initialized for the following: Current web: '{0}' Current user: '{1}' Current request: '{2}'", CurrentWeb.Url, CurrentUser.LoginName, CurrentRequest.ToString( ) );
        }

        private void WorkflowPromoteInfo_Completed( object sender, EventArgs e )
        {
            CustomLoggingService.LogMessage( CustomLoggingService.Area.WorkflowInfrastructure, "Branch promotion workflow completed for the following: Current web: '{0}' Current user: '{1}' Current request: '{2}'", CurrentWeb.Url, CurrentUser.LoginName, CurrentRequest.ToString( ) );
        }
    }
}